<?php

class UsersController extends AppController {

    var $name = 'Users';
    var $uses = array('User', 'FocusArea', 'UserArea', 'Program');
    var $components = array('AjaxUploader');
    var $helpers = array('Html', 'Form', 'Ajax');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allowedActions = array('upload', 'register', 'success', 'add', 'emailcheck', 'usercheck', 'login', 'popup_login', 'genRandomString', 'forget_password', 'backstage_login', 'public_profile');
        //$this->Auth->allowedActions = array('*');      
    }

    function index() {
        //  $this->User->recursive = 0;
        //$this->set('users', $this->paginate());
        $this->redirect(array('action' => 'profile'));
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'profile'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    function usercheck() {
        $this->layout = false;
        $username = $_POST['username'];
        $check = $this->User->find('count', array('conditions' => array('User.username' => $username)));
        if ($check == 0)
            echo 'true';
        die();
    }

    function emailcheck() {
        $this->layout = false;
        $email = $_POST['email'];
        $check = $this->User->find('count', array('conditions' => array('User.email' => $email)));
        if ($check == 0)
            echo 'true';
        die();
    }

    function success() {
        $this->layout = false;
    }

    function add() {
        $this->layout = false;
        // pr($this->Session->read());
        $this->Session->write('profile_images', "");
        $this->Session->write('inspiration_images', "");
        $this->set('focus_areas', $this->FocusArea->find('list'));
    }

    function register() {
        $this->layout = false;
        $errors1 = "";
        $errors = array();
        $return_array = "";

        if ($this->Session->read('profile_images') == "") {
            //$errors1['profile_images'] = "You have to select a profile image.";
            $this->data['User']['profile_image'] = "";
        } else {
            $this->data['User']['profile_image'] = $this->Session->read('profile_images');
        }

        if ($this->Session->read('inspiration_images') == "") {
            //$errors1['inspiration_images'] = "You have to select an inspiration image.";
            $this->data['User']['inspiration_image'] = "";
        } else {
            $this->data['User']['inspiration_image'] = $this->Session->read('inspiration_images');
        }


        if (!empty($this->data)) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $user_id = $this->User->getlastinsertid();
                $i = 0;

                foreach ($this->data['User']['focus_area_id'] as $focus_id) {
                    $this->data['UserArea'][$i]['focus_area_id'] = $focus_id;
                    $this->data['UserArea'][$i]['user_id'] = $user_id;
                    $i++;
                }

                $this->UserArea->create();
                $this->UserArea->saveAll($this->data['UserArea']);
                $return_array['success'] = "The user has been saved successfully.";
            } else {
                $errors = $this->User->invalidFields();
            }
        }

        $return_array['errors'] = $errors;
        echo json_encode($return_array);
        $this->autoRender = false;
    }

    function upload() {
        $this->layout = false;
        $result = $this->AjaxUploader->handleUpload();
        // to pass data through iframe you will need to encode all html tags
        echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
        $this->autoRender = false;
    }

    function edit_profile($id = null) {
        // $this->layout = false;  
        $this->Session->write('profile_images', "");
        $this->Session->write('inspiration_images', "");
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid user', true));
        }

        if (empty($this->data)) {
            $this->User->unbindModel(
                    array('hasMany' => array('Corporation', 'Enroll', 'JidoComment', 'ProductComment', 'Product', 'Program', 'TeamMember', 'Team'))
            );
            $this->User->recursive = 1;
            $this->data = $this->User->read(null, $id);
        }

        $this->set('focus_areas', $this->FocusArea->find('list'));
    }

    function edit($id = null) {
        $errors1 = "";
        $errors = array();
        $return_array = "";
        if ($this->Session->read('profile_images') == "") {
            $this->data['User']['profile_image'] = "";
        } else {
            $this->data['User']['profile_image'] = $this->Session->read('profile_images');
        }

        if ($this->Session->read('inspiration_images') == "") {
            $this->data['User']['inspiration_image'] = "";
        } else {
            $this->data['User']['inspiration_image'] = $this->Session->read('inspiration_images');
        }

        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $user_id = $this->data['User']['id'];
                $i = 0;
                $this->UserArea->deleteAll(array('user_id' => $user_id));
                foreach ($this->data['User']['focus_area_id'] as $focus_id) {
                    $this->data['UserArea'][$i]['focus_area_id'] = $focus_id;
                    $this->data['UserArea'][$i]['user_id'] = $user_id;
                    $i++;
                }
                $this->UserArea->saveAll($this->data['UserArea']);
                $return_array['success'] = "The user has been updated successfully.";
            } else {
                $errors = $this->User->invalidFields();
            }
        }

        $return_array['errors'] = $errors;
        echo json_encode($return_array);
        $this->autoRender = false;
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for user', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->User->delete($id)) {
            $this->Session->setFlash(__('User deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function backstage_index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    function backstage_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    function backstage_add() {
        if (!empty($this->data)) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
    }

    function backstage_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
    }

    function backstage_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for user', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->User->delete($id)) {
            $this->Session->setFlash(__('User deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function login() {
        //$this->layout=false;
        if ($this->Auth->User('id')) {
            $this->redirect(array('action' => 'profile'));
        }
    }

    function popup_login() {
        //$this->layout=false;

        if (!empty($this->data) && $this->Auth->user()) {
            //$this->redirect($this->Auth->redirect());
            $this->redirect(array('action' => 'profile'));
        }
    }

    function logout() {
        $this->redirect($this->Auth->logout());
    }

    function profile() {
        $this->layout = 'innerhome';


        // pr($profileID);
        /* show user focus-area */
        $conditions = array("UserArea.user_id" => $this->Session->read('Auth.User.id'));
        $this->set('user_areas', $this->UserArea->find('all', array('conditions' => $conditions)));

        /* count current age */
        $birthdate = $this->Session->read('Auth.User.birthday');
        $toDay = date("Y-m-d");
        $totalDays = strtotime($toDay) - strtotime($birthdate);
        $convertDays = floor($totalDays / (60 * 60 * 24));
        $currentAge = floor($convertDays / 365);
        $this->set('currentAge', $currentAge);

        /* focus area list */
        $this->set('focus_areas', $this->FocusArea->find('list'));
    }

    function public_profile($id=null) {


        $this->layout = 'innerhome';
        $this->User->recursive = -1;
        $profileID = $this->User->read(null, $id);

        $this->set('profileID', $profileID);

        $this->set('focus_areas', $this->FocusArea->find('list'));
    }

    //User Inbox
    function inbox() {
        $this->layout = false;
    }

    /* function create_program() {


      if (!empty($this->data)) {
      $this->Program->create();
      if ($this->Program->save($this->data)) {
      $this->Session->setFlash(__('The focus area has been saved', true));

      } else {
      $this->Session->setFlash(__('The focus area could not be saved. Please, try again.', true));
      }

      }

      } */

    function start_team_up() {
        
    }

    function invite_from_fb() {
        
    }

    function jido_programs() {
        
    }

    function share() {
        
    }

    function show_all() {
        
    }

    function calender() {
        $this->layout = false;
    }

    function programs() {
        $this->layout = false;
    }

    function graphs() {
        $this->layout = false;
    }

    function team_ups() {
        $this->layout = false;
    }

    function congratulations() {
        $this->layout = false;
    }

    function wills() {
        $this->layout = false;
    }

    function program_details() {
        $this->layout = false;
    }

    function enroll_program() {
        $this->layout = false;
    }

    function redo() {
        $this->layout = false;
    }

    function comments() {
        $this->layout = false;
    }

    function general() {
        $this->layout = false;
    }

    function proteges() {
        $this->layout = false;
    }

    function products() {
        $this->layout = false;
    }

//    function  userid(){
//        $this->layout = 'innerhome';
//    }
}